package org.kjtc.controller;


import org.kjtc.service.MapFindingPileService;
import org.kjtc.service.WxMpService;
import org.kjtc.weChatUtil.CommonUtil;
import org.kjtc.weChatUtil.WXUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Author: xiaojinlu1990@ 163.com
 * @Date: 2019/3/11 0011.
 * @Version 1.0
 * 地图找桩
 */
@Controller
public class MapFindingPileController {
    Logger logger = LoggerFactory.getLogger(MapFindingPileController.class);
    @Autowired
    private WxMpService wxMpService;

    @Resource
    private WXUtils wxUtils;

    @Autowired
    private MapFindingPileService mapFindingPileService;

    @RequestMapping("/mapPile")
    public String mapPile(){
        return "mapPile/mapFindingPile";
    }

    /**
     * 测试 TODO
     * @param request
     * @return
     */
    @RequestMapping("/position")
    public String position(HttpServletRequest request){
        String code = request.getParameter("code");
        logger.info("微信"+code);
        // 获取access_token和openid
       // com.alibaba.fastjson.JSONObject jsonToken = wxUtils.getJsapiAccessTokenByCode(code);
        if (request.getSession() != null && request.getSession().getAttribute(CommonUtil.SESSION_USER) != null) {
            System.out.println( request.getSession().getAttribute(CommonUtil.SESSION_USER));
        }
        return "mapPile/position";
    }

    @RequestMapping("/getJssdk")
    @ResponseBody
    public Object getJssdk(@RequestParam(value = "url",required = false ) String url,
                           @RequestParam(value = "longitude",required = false ) String longitude,
                           @RequestParam(value = "latitude",required = false ) String latitude,
                           @RequestParam(value = "chargingstationname",required = false ) String chargingstationname,
                           @RequestParam(value = "chargingstationaddress",required = false ) String chargingstationaddress
                           ){
        return wxMpService.getJsjdk(url,longitude,latitude,  chargingstationname, chargingstationaddress);
    }

    /***
     * 获取用户实时位置
     * @param url
     * @return
     */
    @RequestMapping("/userRealTimeLocation")
    @ResponseBody
    public Object userRealTimeLocation(@RequestParam(value = "url",required = false ) String url){
        return wxMpService.getJsjdk(url,"","","","");
    }

    /**
     * 获取首页站桩信息显示
     * @param type
     * @param url
     * @return
     */
    @RequestMapping("/collection/{type}")
    @ResponseBody
    public Object getcollectionJssdk(@PathVariable(value = "type") String type,
                                     @RequestParam(value = "url",required = false) String url,
                                     @RequestParam(value = "longitude",required = false) String longitude,
                                     @RequestParam(value = "latitude",required = false) String latitude,
                                     @RequestParam(value = "openid",required = false) String openid
                                     ){
        List<Map<String,Object>>  list = null;
        if ("lately".equals(type))
            list = mapFindingPileService.collectionLately(longitude,latitude,openid);
        else if ("bestHigth".equals(type))
            list = mapFindingPileService.collectionestHigth();
        else if ("privateChareStation".equals(type))
            list = mapFindingPileService.collectionMessage();
        else
            logger.info("未发现该服务");

        return list;
    }
    /**
     * 获取所以站信息
     * @return
     */

    @PostMapping("chargingstationList")
    @ResponseBody
    public Object chargingstationList(@RequestParam(value = "url",required = false) String url){
        return mapFindingPileService.chargingstationList();
    }
    /**
     * 收藏接口
     * @return
     */

    @RequestMapping("mpCollectionStationMassage/{ChargingStationID}/{openid}")
    @ResponseBody
    public Object mpCollectionStationMassage(@PathVariable(value = "ChargingStationID") String chargingstationid,
                                             @PathVariable(value = "openid") String openid,
                                             @RequestParam(value = "type",required = false) String type){
        return mapFindingPileService.mpCollectionStationMassage(chargingstationid,openid,type);
    }


    /**
     * 微信服务器设置验证文件
     * @param url
     * @return
     */
    @RequestMapping("MP_verify_TNKF21UjkvEREIHa.txt")
    @ResponseBody
    public Object html(String url){
        return "TNKF21UjkvEREIHa";
    }
}
